﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Linq.Expressions;

using FinalProject.DAL.OM;

namespace FinalProject.BL
{
    public class ManagerDocumentBL
    {
        private static readonly string strConnect = System.Configuration.ConfigurationManager.ConnectionStrings["QLBHEntities"] != null ?
                                                                     System.Configuration.ConfigurationManager.ConnectionStrings["QLBHEntities"].ConnectionString : null;

        public static IList<DocumentItem> GetALLDocumentItems()
        {
            using (QLBHEntities1 entity = new QLBHEntities1(strConnect))
            {
                IList<DocumentItem> result = null;
                var query = from hd in entity.HoaDons
                            //from kh in entity.KhachHangs
                            //from nv in entity.NhanViens
                            //where hd.KhachHang.ID == kh.ID &&
                            //          hd.NhanVien.ID == nv.ID
                            select new DocumentItem
                            {
                                InvoiceID = hd.ID,
                                EmployeeID = hd.KhachHang_ID,
                                CustomerID = hd.NhanVien_ID,
                                InvoiceCode = hd.MaHD,
                                EmployeeCode = hd.NhanVien.MaNV,
                                CustomerCode = hd.KhachHang.MaKH,
                                CreateDate = hd.NgayLap ?? DateTime.Now,
                                TotalAmount = hd.TongTien ?? decimal.Zero

                            };
                result = query.ToList();
                return result;
            }
        }

        public static IList<DocumentDetailItem> GetAllDetail(Guid invoiceId)
        {
            using (QLBHEntities1 context = new QLBHEntities1(strConnect))
            {
                var query = from q in context.ChiTietHoaDons.Include("HoaDon").Include("SanPham")
                            where q.HoaDon.ID == invoiceId
                            select new DocumentDetailItem
                            {
                                ItemID = q.SanPham.ID,
                                ItemCode = q.SanPham.MaSP,
                                Quantity = q.SoLuong ?? 0
                            };

                return query.ToList();
            }
        }

        public static IDictionary<Guid, string> GetAllCustomerCode()
        {
            using (QLBHEntities1 entities = new QLBHEntities1(strConnect))
            {
                IDictionary<Guid, string> result = null;
                result = entities.KhachHangs.ToDictionary(p => p.ID, g => g.TenKH);

                return result;
            }
        }

        public static IDictionary<Guid, string> GetAllEmployeeCode()
        {
            using (QLBHEntities1 entities = new QLBHEntities1(strConnect))
            {
                IDictionary<Guid, string> result = null;
                result = entities.NhanViens.ToDictionary(p => p.ID, g => g.TenNV);
                return result;
            }
        }

        public static IDictionary<Guid, string> GetAllItemCode()
        {
            using (QLBHEntities1 entities = new QLBHEntities1(strConnect))
            {
                IDictionary<Guid, string> result = null;
                result = entities.SanPhams.ToDictionary(p => p.ID, g => g.MaSP);
                return result;
            }
        }

        private static decimal GetTotalAmount(IList<DocumentDetailItem> abc, QLBHEntities1 context)
        {
            decimal total = decimal.Zero;
            foreach (var item in abc)
            {
                total += context.SanPhams
                                        .Where(p => p.ID == item.ItemID)
                                        .Select(p => p.DonGia)
                                        .FirstOrDefault() ?? decimal.Zero * item.Quantity;
            }
            return total;
        }

        public static void CreateNewInvoice(DocumentItem docItem, IList<DocumentDetailItem> docDetail)
        {
            using (QLBHEntities1 context = new QLBHEntities1(strConnect))
            {
                HoaDon invoice = new HoaDon()
                {
                    ID = Guid.NewGuid(),
                    MaHD = docItem.InvoiceCode,
                    KhachHang_ID = docItem.CustomerID,
                    NhanVien_ID = docItem.EmployeeID,
                    NgayLap = docItem.CreateDate,
                    TongTien= GetTotalAmount(docDetail, context)
                };
                context.AddToHoaDons(invoice);

                foreach (DocumentDetailItem item in docDetail)
                {
                    ChiTietHoaDon invoiceDetail = new ChiTietHoaDon()
                    {
                        ID = Guid.NewGuid(),
                        SP_ID = item.ItemID,
                        HoaDon = invoice,
                        SoLuong = item.Quantity
                    };
                    context.AddToChiTietHoaDons(invoiceDetail);
                }

                context.SaveChanges();
            }
        }

        public static bool CheckExist(string strInvoiceCode)
        {
            bool result = false;

            using (QLBHEntities1 context = new QLBHEntities1(strConnect))
            {
                var query = from q in context.HoaDons
                            where q.MaHD == strInvoiceCode
                            select q;

                result = query.FirstOrDefault() != null;
            }

            return result;
        }

        public static decimal LoadItemAmount(Guid itemID)
        {
            using (QLBHEntities1 context = new QLBHEntities1(strConnect))
            {
                var query = from q in context.SanPhams
                            where q.ID == itemID
                            select q.DonGia;
                return query.First() ?? decimal.Zero;
            }
        }

        public static void DeleteInvoice(Guid invoiceID)
        {
            using (QLBHEntities1 context = new QLBHEntities1(strConnect))
            {


                HoaDon delInvoice = context.HoaDons.Include("ChiTietHoaDons").Where(p => p.ID == invoiceID).FirstOrDefault();
                if (delInvoice != null)
                {
                    delInvoice.ChiTietHoaDons.Clear();

                    context.DeleteObject(delInvoice);
                }
                context.SaveChanges();
            }
        }


    }

    public class DocumentItem
    {
        public Guid InvoiceID { get; set; }
        public Guid EmployeeID { get; set; }
        public Guid CustomerID { get; set; }
        public string InvoiceCode { get; set; }
        public string EmployeeCode { get; set; }
        public string CustomerCode { get; set; }
        public DateTime CreateDate { get; set; }
        public decimal TotalAmount { get; set; }
    }

    public class DocumentDetailItem
    {
        public Guid ItemID { get; set; }
        public string ItemCode { get; set; }
        public int Quantity { get; set; }
    }
}
